package org.qydjk.console.service.impl;

import org.qydjk.common.dao.IBaseDao;
import org.qydjk.common.pojo.product.ProductAttr;
import org.qydjk.common.service.impl.BaseService;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.service.IAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  属性Service实现
 * Created by Edward_yang on 2017/12/13 0013.
 */
@Service
public class AttributeService  extends BaseService  implements IAttributeService{
    @Autowired
    private IBaseDao baseDao;

    /****
     *  带分页查询和筛选属性
     ** @author Edward_yang
     * @uptime 2017-12-12
     * @param page 当前页
     * @param rows 每页多少条
     * @param one_category_id  联动
     * @param keyWord  查询条件
     * @return EasyUIResponse total和List
     */
    public EasyUIResponse findAttrbuteList(Integer page, Integer rows, String one_category_id, String keyWord)throws Exception {
        HibernatePage pager = new HibernatePage(page,rows);
        String sql=" select * from qy_product_attr apa ";
        String hqlCount="select count(pa.id) from ProductAttr pa ";
        //联动处理
        if(!StringUtil.isEmptyOrNull(one_category_id)){
            sql+=" where apa.one_category_id="+one_category_id;
            hqlCount+=" where pa.oneCategoryId='"+one_category_id+"'";
        }
        //查询
        if(!StringUtil.isEmptyOrNull(keyWord)){
            sql+="  where apa.name like '%"+keyWord+"%' or  apa.code like '%"+keyWord+"%'";
            hqlCount+=" where pa.name like '%"+keyWord+"%' or  pa.code like '%"+keyWord+"%'";
        }
        sql+= " order by apa.ord desc ";

            List<ProductAttr> ProductAttrList =baseDao.findBySQL(sql, ProductAttr.class,pager);
            int total = baseDao.getRecordCountHQL(hqlCount);
            return new EasyUIResponse(total,ProductAttrList);
    }
}
